<!doctype html>
<meta charset=utf-8>
<title>XMLHttpRequest: data uri</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
    <link rel="help" href="https://xhr.spec.whatwg.org/#data:-urls-and-http" data-tested-assertations="following::ul/li[1] following::ul/li[2] following::ul/li[4]" />
    <link rel="help" href="https://xhr.spec.whatwg.org/#the-send()-method" data-tested-assertations="following::ul/li[10]/dl/dt[2]" />
<div id="log"></div>

<script>
  function do_test(method, uri, charset, testNamePostfix) {
    if (typeof charset === 'undefined' || charset === null) charset = 'text/plain';
    var test = async_test("XHR method " + method + " with charset " + charset+(testNamePostfix||''));
    test.step(function() {
      var client = new XMLHttpRequest();
      client.onreadystatechange = test.step_func(function () {
        if (client.readyState !== 4) {
          return;
        }

        // Note: fetching a data URL with a non-GET method returns a network
        // error per <http://fetch.spec.whatwg.org/#basic-fetch>.
        if (method.toUpperCase() !== 'GET') {
          assert_equals(client.status, 0);
          assert_equals(client.responseText, '');
          assert_equals(client.statusText, 'OK');
          test.done();
          return;
        }

        assert_equals(client.responseText, "Hello, World!");
        assert_equals(client.status, 200);
        assert_equals(client.getResponseHeader('Content-Type'), charset);
        var allHeaders = client.getAllResponseHeaders();
        assert_regexp_match(allHeaders, /content\-type\:/i, 'getAllResponseHeaders() includes Content-Type');
        assert_false(/content\-length\:/i.test(allHeaders), 'getAllResponseHeaders() must not include Content-Length');
        test.done();
      });
      client.open(method, uri);
      client.send(null);
    });
  }
  do_test('GET', "data:text/plain,Hello, World!");
  do_test('GET', "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==", undefined, " (base64)");
  do_test('GET', "data:text/html,Hello, World!", 'text/html');
  do_test('GET', "data:text/html;charset=UTF-8,Hello, World!", 'text/html;charset=UTF-8');
  do_test('GET', "data:image/png,Hello, World!", 'image/png');
  do_test('POST', "data:text/plain,Hello, World!");
  do_test('PUT', "data:text/plain,Hello, World!");
  do_test('DELETE', "data:text/plain,Hello, World!");
  do_test('HEAD', "data:text/plain,Hello, World!");
  do_test('UNICORN', "data:text/plain,Hello, World!");
</script>
